package com.atguigu.spzx.manager.service.impl;

import com.alibaba.excel.EasyExcel;
import com.atguigu.spzx.manager.listenner.EasyExcelListener;
import com.atguigu.spzx.manager.mapper.CategoryMapper;
import com.atguigu.spzx.manager.service.FileUploadService;
import com.atguigu.spzx.manager.utils.MinioProperties;
import com.atguigu.spzx.model.dto.product.CategoryExcelDto;
import io.minio.*;
import org.joda.time.DateTime;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;

@Service
@Transactional
public class FileUploadServiceImpl implements FileUploadService {
    @Autowired
    private MinioProperties minioProperties;

    @Autowired
    private CategoryMapper categoryMapper;

    /*
     * @description: 头像上传
     **/
    @Override
    public String avatarUpload(MultipartFile file) {
        return imageUpload(file, "avatar");
    }

    /*
     * @description: easyExcel导入
     **/
    @Override
    public void categoryFileUpload(MultipartFile file) {
        try {
            EasyExcel.read(file.getInputStream(), CategoryExcelDto.class, new EasyExcelListener(categoryMapper)).sheet("分类数据").doRead();
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
    }

    /*
     * @description: 品牌图标上传
     **/
    @Override
    public String logoUpload(MultipartFile file) {
        return imageUpload(file, "logo");
    }

    /*
     * @description: 品牌轮转图上传
     **/
    @Override
    public String sliderUpload(MultipartFile file) {
        return imageUpload(file, "slider");
    }

    /*
     * @description: 品牌轮转图取消上传
     **/
    @Override
    public void sliderRemove(String fileName) {
        try{
            //1、 配置 登录 初始化文件
            MinioClient minioClient = MinioClient.builder()
                    .endpoint(minioProperties.getMinUrl())
                    .credentials(minioProperties.getMinLoginAccount(), minioProperties.getMinLoginPassword())
                    .build();
            // 判断桶是否存在
            boolean found = minioClient.bucketExists(BucketExistsArgs.builder().bucket(minioProperties.getBucketName()).build());
            if (!found) {       // 如果不存在，那么此时就创建一个新的桶
                minioClient.makeBucket(MakeBucketArgs.builder().bucket(minioProperties.getBucketName()).build());
            } else {  // 如果存在打印信息
                System.out.println("Bucket 'spzx-bucket' already exists.");
            }
            RemoveObjectArgs removeObjectArgs = RemoveObjectArgs.builder()
                    .bucket(minioProperties.getBucketName())
                    .object(fileName)
                    .build();
            minioClient.removeObject(removeObjectArgs);
        }catch (Exception e){
            throw new RuntimeException(e);
        }
    }

    @Override
    public String detailUpload(MultipartFile file) {
        return imageUpload(file,"detail");
    }

    private String imageUpload(MultipartFile file, String modelName) {
        try {
//        1、 配置 登录 初始化文件 选择桶
            MinioClient minioClient = MinioClient.builder()
                    .endpoint(minioProperties.getMinUrl())
                    .credentials(minioProperties.getMinLoginAccount(), minioProperties.getMinLoginPassword())
                    .build();
            // 判断桶是否存在
            boolean found = minioClient.bucketExists(BucketExistsArgs.builder().bucket(minioProperties.getBucketName()).build());
            if (!found) {       // 如果不存在，那么此时就创建一个新的桶
                minioClient.makeBucket(MakeBucketArgs.builder().bucket(minioProperties.getBucketName()).build());
            } else {  // 如果存在打印信息
                System.out.println("Bucket 'spzx-bucket' already exists.");
            }
            // 设置存储对象名称
//            String extFileName = FileNameUtils.getExtension(file.getOriginalFilename());
            String timeStr = new DateTime().toString("/yyyy/MM/dd/");
            String originalFilename = file.getOriginalFilename();
            PutObjectArgs putObjectArgs = PutObjectArgs.builder()
                    .bucket(minioProperties.getBucketName())
                    .stream(file.getInputStream(), file.getSize(), -1)
                    .object(modelName + timeStr + originalFilename)
                    .build();

//        2、 上传
            minioClient.putObject(putObjectArgs);
//        3、获取访问路径
            return minioProperties.getMinUrl() + "/" + minioProperties.getBucketName() +
                    "/" + modelName + timeStr + originalFilename;
        } catch (Exception e) {
            throw new RuntimeException(e);
        }

    }
}
